package com.dragon.app.concurrent;

import java.util.concurrent.*;

import static java.util.concurrent.Executors.newSingleThreadExecutor;

/**
 * 
 * <p>
 * class: ExecutorTest
 * </p>
 * <p>
 * Description: 线程池测试
 * </p>
 * 
 * @author SmallDragon
 * @date 2019年5月7日
 *
 */
public class ExecutorTest2 {

	public static void main(String[] args) {

		//自定义线程池,推荐使用这种方式创建线程池。
		ExecutorService executorService = new ThreadPoolExecutor(
				2,
				3,
				10,
				TimeUnit.SECONDS,
				new LinkedBlockingQueue<>(5),
				new ThreadPoolUtils.MyThreadFactory(),
				new ThreadPoolExecutor.DiscardPolicy());


		for (int i = 0; i < 2; i++) {
			executorService.execute(()->{
				System.out.println(Thread.currentThread().getName() + ":开始办理业务");
				try {
					TimeUnit.MILLISECONDS.sleep((long) Integer.MAX_VALUE);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}


		try {
			for (int i = 0; i < 10; i++) {
				executorService.execute(() -> {
					System.out.println(Thread.currentThread().getName() + ":开始业务");
					try {
						TimeUnit.MILLISECONDS.sleep((long) (Math.random() * 1000) + 1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName() + ":===业务结束===");
				});
			}
			System.out.println("main调用线程池完成。。。。。");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			executorService.shutdown();
		}

		System.out.println("main结束。。。。。");

	}

}
